package org.hydrakyoufeng.dm.toolkit;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;

import org.hydrakyoufeng.log.LogTool;
import org.hydrakyoufeng.tool.collection.MapUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 日志线程
 * @author HydraKyouFeng
 *
 */
@Component
public class LogThread extends Thread {
	
//	@Autowired
//	private ApiLogDao apiLogDao;
	
//	@Value("${myconfig.logSwitch}")
//	private boolean logSwitch; 
	
	@Value("${myconfig.logParameter}")
	private boolean logParameter; 
	
	@Value("${myconfig.logResult}")
	private boolean logResult; 
	
	public LogThread() {
		start();
	}

	/**
	 * 数据队列
	 */
	private ArrayBlockingQueue<Map<String,Object>> queue = new ArrayBlockingQueue<>(512);
	
	@Override
	public void run() {
		while (true) {
			Map<String,Object> map = null;
			try {
				map = queue.take();
				if (logParameter) {
					LogTool.log(LogTool.INFO, map.get("request"));
				}
				if (logResult) {
					LogTool.log(LogTool.INFO, map.get("response"));
				}
//				if (logSwitch) {
//					apiLogDao.insert(map);
//				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			MapUtil.offerHashMap(map);
		}
	}
	
	/**
	 * 将日志数据放入到队列中
	 * @param e
	 */
	public void offer(Map<String,Object> e) {
		queue.offer(e);
	}
}
